//
//  CYVideoCollectViewController.m
//  KCZ_B段项目
//
//  Created by dllo on 15/11/11.
//  Copyright © 2015年 Sky. All rights reserved.
//

#import "CYVideoCollectViewController.h"
#import "FitHeader.h"
#import "DataBaseManager.h"
#import "NextSpecialViewController.h"
@interface CYVideoCollectViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSString *titleString;
@property(nonatomic, strong)NSString *idString;
@property(nonatomic, strong)NSMutableArray *dataArray;
@end

@implementation CYVideoCollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"菜谱收藏";
}

-(void)loadView
{
    [super loadView];
    [self getData];
  
}

-(void)getData
{
    _dataArray = [DataBaseManager selectAllSpecial];
    if (_dataArray.count == 0) {
        UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(100 * wt, 100 * ht, 175 *wt, 30 * ht)];
        lable.text = @"你还没有收藏噢~";
        lable.textAlignment = 1;
        lable.textColor =  [UIColor colorWithRed:0.19 green:0.75 blue:0.6 alpha:1];
        [self.view addSubview:lable];
        
    }
    else
    {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableFooterView = [[UITableView alloc]init];
        [self.view addSubview:_tableView];
        [_tableView reloadData];
    }
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = [_dataArray[indexPath.row] Title];
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NextSpecialViewController *nvc = [[NextSpecialViewController alloc]init];
    nvc.tempModel = _dataArray[indexPath.row];
    [self.navigationController pushViewController:nvc animated:YES];
}

//允许编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

//编辑
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:YES];
    [self.tableView setEditing:editing animated:YES];
}

//编辑样式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

//删除方法
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [DataBaseManager deleteSpecialByTitle:[_dataArray[indexPath.row] Title]];
        [_tableView removeFromSuperview];
        [self getData];
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
